/*
 *  Copright: 2008 Philipp Leitner, Distributed Systems Group,
 *            TU Wien, leitner@infosys.tuwien.ac.at
 *
 *  This file is part of DAIOS.
 *
 *  DAIOS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation.
 * 
 *  DAIOS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with DAIOS.  If not, see
 *  <http://www.gnu.org/licenses/>.
 * 
 */

package at.ac.tuwien.infosys.dsg.daios.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * A helper class used to encode objects for network
 * submission.
 * 
 * <p>
 * 
 * The actual base64 encoder used (
 * <link>at.ac.tuwien.infosys.dsg.daios.util.Base64</link>
 * ) is by Roedy Green. See the source code for copyright
 * details.
 * 
 * @author philipp
 *
 */
public class EncodingHelper {
	
	/**
	 * Private constructor.
	 *
	 */
	private EncodingHelper() {
	}
	
	/**
	 * Factory method. Get a new encoder instance.
	 * 
	 * @return The instance.
	 */
	public static EncodingHelper getInstance() {
		
		return new EncodingHelper();
		
	}
	
	/**
	 * Encode an array of bytes as base64.
	 * 
	 * @param bytes The bytes to encode.
	 * @return An base64-encoded String
	 * representing the given Byte[]
	 */
	public String base64Encode(Byte[] bytes){
		
		// wrap the Byte[] into a byte[]
		byte[] newBytes = new byte[bytes.length];
		int i = 0;
		for(Byte b:bytes)
			newBytes[i++] = b.byteValue();

		return Base64.encodeBytes(newBytes);
	
	}

	/**
	 * Decode a base64-encoded String back into
	 * bytes.
	 * 
	 * @param s The encoded String.
	 * @return The bytes represented by the given String.
	 */
	public Byte[] base64Decode(String s) {
		
		byte[] bytes = Base64.decode(s);
		
		// wrap the byte[] into a Byte[]
		Byte[] newBytes = new Byte[bytes.length];
		int i = 0;
		for(byte b:bytes)
			newBytes[i++] = b;
		
		return newBytes;
		
	}
	
	/**
	 * Encode a String as defined by the
	 * application/x-www-form-urlencoded MIME standard.
	 * 
	 * @param s The String to encode.
	 * @return The URL-encoded String.
	 */
	public String urlEncode(String s) {
		
		try {
			return URLEncoder.encode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// hopefully this does never happen :)
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	/**
	 * Decode a String which is encoded as defined by the
	 * application/x-www-form-urlencoded MIME standard.
	 * 
	 * @param s The encoded String.
	 * @return The decoded String.
	 */
	public String urlDecode(String s) {
		
		try {
			return URLDecoder.decode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// hopefully this does never happen :)
			e.printStackTrace();
		}
		
		return null;
		
	}
}
